"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 普通用户执行gs_upgradechk check -p port -F csv/doc/xls等，
              导出的报告指定非markdown格式，合理报错
Create At   : 2023/08/02
Owner       : opentestcase042
Description :
    1.执行gs_upgradechk check -p port -F csv
    2.执行gs_upgradechk check -p port -F doc
    3.执行gs_upgradechk check -p port -F xls
Expect      :
    1.导出报错失败
    2.导出报错失败
    3.导出报错失败
History     :
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class ToolsTestCase(unittest.TestCase):
    def setUp(self):
        self.common = Common()
        self.commonsh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        LOG.info("---SetUp：检查数据库状态是否正常---")
        status = self.commonsh.get_db_cluster_status()
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_system_internal_tools(self):
        LOG.info(f"----{os.path.basename(__file__)} start----")
        LOG.info("---step1：执行gs_upgradechk check -p port -F csv\
                            expect:导出失败---")
        cmd1 = f"gs_upgradechk check -p {self.user_node.db_port} -F csv"
        LOG.info(cmd1)
        result1 = self.user_node.sh(f"source {macro.DB_ENV_PATH};"
                                       f"{cmd1}").result()
        LOG.info(result1)
        self.assertIn('ERROR： 错误的格式参数report-format csv,当前仅支持markdown格式',\
                      result1)

        LOG.info("---step2：gs_upgradechk check -p port -F doc\
                                    expect:导出失败---")
        cmd2 = f"gs_upgradechk check -p {self.user_node.db_port} -F doc"
        LOG.info(cmd2)
        result2 = self.user_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd2}").result()
        LOG.info(result2)
        self.assertIn('ERROR： 错误的格式参数report-format doc,当前仅支持markdown格式',\
                      result2)

        LOG.info("---step3：执行gs_upgradechk check -p port -F xls\
                                    expect:导出失败---")
        cmd3 = f"gs_upgradechk check -p {self.user_node.db_port} -F xls"
        LOG.info(cmd3)
        result3 = self.user_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd3}").result()
        LOG.info(result3)
        self.assertIn('ERROR： 错误的格式参数report-format xls,当前仅支持markdown格式', \
                      result3)


    def tearDown(self):
        LOG.info('--------无需清理环境--------')
        status = self.commonsh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f"------{os.path.basename(__file__)} end------")